home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / linewrapper.cpp < prev    next >
C/C++ Source or Header  |  2004-06-29  |  5KB  |  141 lines

  1. /***************************************************************************
  2.                         linewrapper.cpp  -  description
  3.                              -------------------
  4.     begin                : Sa Jan 00 2004
  5.     copyright            : (C) 2004 by Andre Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "linewrapper.h"
  19.  
  20. namespace highlight {
  21.  
  22. LineWrapper::LineWrapper(unsigned int maxlength, bool indentAfterOpenBraces):
  23.  MAX_LINE_LENGTH(maxlength-1), 
  24.  index(0),
  25.  wsPrefixLength(string::npos),
  26.  hasMore(false),
  27.  indentAfterOpenBraces(indentAfterOpenBraces),
  28.  redefineWsPrefix(false)
  29. {  
  30. }
  31.  
  32. LineWrapper::LineWrapper():
  33.  MAX_LINE_LENGTH(80), 
  34.  index(0),
  35.  wsPrefixLength(string::npos),
  36.  hasMore(false),
  37.  indentAfterOpenBraces(true),
  38.  redefineWsPrefix(false)
  39. {  
  40. }
  41.  
  42. LineWrapper::~LineWrapper()
  43. {
  44. }
  45.  
  46. bool LineWrapper::hasMoreLines(){
  47.   return hasMore;
  48. }
  49.  
  50. bool LineWrapper::indentCode(){
  51.   return indentAfterOpenBraces;
  52. }
  53.  
  54. void LineWrapper::setLine(const std::string newLine){
  55.   line=newLine;
  56.   wsPrefix="";  
  57.   index=0;
  58.   wsPrefixLength=string::npos;
  59.   hasMore=true;
  60.   redefineWsPrefix=false;
  61. }
  62.  
  63. std::string  LineWrapper::getNextLine(){        
  64.    
  65.    if (!index && line.length() > MAX_LINE_LENGTH){ // erster Durchlauf...
  66.       // wenn mglich an ffnender Klammer oder Geichheitszeichen ausrichten
  67.       if (indentAfterOpenBraces){
  68.           wsPrefixLength=line.find_first_of("{(=");
  69.       }
  70.       // sonst die Einrckung der Originalzeile beibehalten
  71.       if (wsPrefixLength==string::npos || wsPrefixLength-index>MAX_LINE_LENGTH){
  72.           wsPrefixLength=line.find_first_not_of(WS_CHARS);
  73.       }
  74.       else {
  75.           // wsPrefix in allen neu umgebrochenen Zeilen durch Spaces ersetzen
  76.           redefineWsPrefix=true;
  77.           //  Position hinter ffnende Klammer springen
  78.           wsPrefixLength=line.find_first_not_of(WS_CHARS,wsPrefixLength+1); 
  79.       }
  80.       
  81.       if (wsPrefixLength!=string::npos){
  82.         index = wsPrefixLength;
  83.         // Falls Anzahl der Whitespaces am beginn der ersten zeile gr∩┐╜r
  84.         // als Max. Zeilenl∩┐╜ge, Whitespaces verwerfen
  85.         if (wsPrefixLength>MAX_LINE_LENGTH){   
  86.           wsPrefixLength=0;
  87.           return string();          
  88.         }
  89.         else{          
  90.            wsPrefix=line.substr(0, wsPrefixLength);          
  91.         }
  92.       }
  93.       // Zeile enthaelt nur Whitespace; verwerfen
  94.       else {
  95.        hasMore= false;
  96.        return string();
  97.       }
  98.    } else {
  99.      if (redefineWsPrefix){
  100.        wsPrefix="";
  101.        for (unsigned int i=0;i<wsPrefixLength;i++)
  102.           wsPrefix += " ";
  103.      }
  104.      redefineWsPrefix=false;
  105.    }
  106.  
  107.    string resultString;
  108.    
  109.    // Position, ab der rckwaerts nach Umbruchmglichkeit gesucht wird
  110.    unsigned int searchEndPos=MAX_LINE_LENGTH-wsPrefixLength;
  111.    
  112.    // letztes Teilstueck der Zeile ausgeben; Parsen beenden
  113.    if (line.length()-index < searchEndPos) {
  114.      hasMore=false;
  115.      resultString=(index>0)?wsPrefix + line.substr(index): line.substr(index);
  116.      return resultString;
  117.    }
  118.    
  119.    // Umbrechposition suchen
  120.    unsigned int lbPos = line.find_last_of(LB_CHARS, index+searchEndPos);
  121.    if (lbPos <= index || lbPos == string::npos) {
  122.      // nichts gefunden, hart umbrechen
  123.      lbPos = index + searchEndPos;
  124.    }
  125.    // Einrckung der Originalzeile erhalten
  126.    resultString+=wsPrefix;
  127.    // Neue Zeile erzeugen
  128.    resultString += line.substr(index, lbPos-index+1);
  129.   
  130.    // Whitespace am neuen Zeilenbeginn ignorieren, ausser beim ersten Durchlauf
  131.    //unsigned int newIndex=StringTools::getNextNonWsPos(line,lbPos+1);
  132.    unsigned int newIndex=line.find_first_not_of(WS_CHARS, lbPos+1);
  133.    index=(newIndex!=string::npos)?newIndex:line.length();
  134.  
  135.    hasMore=index!=line.length(); // unnoetigen Leerstring vermeiden
  136.  
  137.    return resultString;
  138. }
  139.  
  140. }
  141.